﻿using LCA.Community.DAL;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using System;
using System.Linq;
using LCA.Community.DAL.Model;
using LCA.Community.DAL.Interfaces;
using System.Collections.Generic;
using LCA.Community.DAL.Test.ModelFactories;
using Microsoft.Practices.Unity;

namespace LCA.Community.DAL.Test
{
    /// <summary>
    ///This is a test class for EventsRepositoryTest and is intended
    ///to contain all EventsRepositoryTest Unit Tests
    ///</summary>
    [TestClass()]
    public class EventsRepositoryTest : BaseTestClass
    {
        private UsersFactory _usersFactory = new UsersFactory();
        private User _user;
        private GroupsFactory _groupsFactory = new GroupsFactory();
        private List<Group> _groups;
        private EventsFactory _eventsFactory = new EventsFactory();
        private List<Event> _events;

        /// <summary>
        /// Initializes the test.
        /// </summary>
        [TestInitialize()]
        public void InitializeTest()
        {
            UnityContainerInitializer();

            var usersRepository = _unityContainer.Resolve<UsersRepository>();
            _user = _usersFactory.CreateEntity();
            usersRepository.Add(_user);
            var groupsRepository = _unityContainer.Resolve<IGroupsRepository>();
            _groups = _groupsFactory.CreateList(3,_user.RowKey);
            _groups.ForEach(g => groupsRepository.Add(g));
            var eventsRepository = _unityContainer.Resolve<IEventsRepository>();
            _events = _eventsFactory.CreateList(5, _groups[0].RowKey);
            _events.ForEach(e => eventsRepository.Add(e));
        }

        /// <summary>
        /// Cleans up the test.
        /// </summary>
        [TestCleanup()]
        public void CleanupTest()
        {
            var usersRepository = _unityContainer.Resolve<IUsersRepository>();
            var groupsRepository = _unityContainer.Resolve<IGroupsRepository>();
            var eventsRepository = _unityContainer.Resolve<IEventsRepository>();

            usersRepository.DeleteTestEntities();
            eventsRepository.DeleteTestEntities();
            groupsRepository.DeleteTestEntities();
        }
        
        /// <summary>
        ///A test for DeleteAllGroupsEvents
        ///</summary>
        [TestMethod()]
        public void DeleteAllGroupsEventsTest()
        {
            var target = _unityContainer.Resolve<IEventsRepository>();
            string groupID = _groups[0].RowKey;

            target.DeleteAllGroupsEvents(groupID);
            var actual = target.GetEventsByGroup(groupID);
            CollectionAssert.AreEquivalent(new List<Event>(), actual);
        }

        /// <summary>
        ///A test for GetEventsByGroup
        ///</summary>
        [TestMethod()]
        public void GetEventsByGroupTest()
        {
            var target = _unityContainer.Resolve<IEventsRepository>();
            string groupID = _groups[0].RowKey;
            List<Event> expected = _events;
            List<Event> actual;

            actual = target.GetEventsByGroup(groupID);
            CollectionAssert.AreEquivalent(expected, actual);
        }

        /// <summary>
        ///A test for GetNextGroupEvents
        ///</summary>
        [TestMethod()]
        public void GetNextGroupEventsTest()
        {
            var target = _unityContainer.Resolve<IEventsRepository>();
            string groupID = _groups[0].RowKey;
            int quantity = 5;

            var events = _eventsFactory.CreateList(quantity, groupID).Where(e => e.Date >= DateTime.Now).OrderByDescending(e => e.Date).ToList();
            events.ForEach(e => target.Add(e));

            List<Event> expected = events;
            List<Event> actual;

            actual = target.GetNextGroupEvents(groupID, quantity);
            CollectionAssert.AreEqual(expected, actual);
        }

        /// <summary>
        ///A test for RecentEvents
        ///</summary>
        [TestMethod()]
        public void RecentEventsTest()
        {
            var target = _unityContainer.Resolve<IEventsRepository>();
            var expected = _eventsFactory.CreateList(10, _groups[0].RowKey);
            expected.ForEach(e => target.Add(e));
            expected = expected.OrderByDescending(e => e.Timestamp).ToList();

            List<Event> actual;
            actual = target.RecentEvents;
            CollectionAssert.AreEquivalent(expected, actual);
        }
    }
}
